package struct.queue.realize;

import java.util.NoSuchElementException;

/**
 * @author: 柒月
 * @date: 2025/5/2 下午4:52
 * @version: 1.0
 */
public class ListQueue {

    private Node head;
    private Node tail;
    private int size;

    public ListQueue() {
        head = tail = null;
        size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int size() {
        return size;
    }

    public void offer(int value) {
        Node newNode = new Node(value);
        if (isEmpty()) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
        size++;
    }

    public int poll() {
        if (isEmpty()){
            throw new NoSuchElementException();
        }else {
            int value = head.data;
            head = head.next;
            size--;
            return value;
        }
    }

    public int peek() {
        if (isEmpty()){
            throw new NoSuchElementException();
        }else {
            return head.data;
        }
    }


    private static class Node {

        private final int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }
    }

}
